package com.customization.api.e9sso.biz;

import com.customization.commons.Console;
import com.engine.common.util.ParamUtil;
import com.engine.workflow.biz.freeNode.FreeNodeBiz;
import com.engine.workflow.util.CommonUtil;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import weaver.conn.RecordSet;
import weaver.file.ExcelFile;
import weaver.file.ExcelRow;
import weaver.file.ExcelSheet;
import weaver.file.ExcelStyle;
import weaver.fna.general.FnaCommon;
import weaver.fna.maintenance.FnaCostCenter;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.company.SubCompanyComInfo;
import weaver.hrm.resource.ResourceComInfo;
import weaver.systeminfo.SystemEnv;
import weaver.workflow.mode.FieldInfo;
import weaver.workflow.request.RequestDetailImport;

public class CusDetailTemplateDownloadBiz {
    private String filename;

    public CusDetailTemplateDownloadBiz() {
    }

    public InputStream templateDownload(HttpServletRequest var1, HttpServletResponse var2) throws Exception {
        User var3 = CommonUtil.getUserByRequest(var1, var2);
        ExcelFile var4 = this.createExcelFile(var1, var2, var3);
        if (var4 == null) {
            return null;
        } else {
            this.filename = var4.getFilename();
            String var5 = var3.getLanguage() + "";
            ExcelSheet var6 = null;
            ExcelRow var7 = null;
            HSSFWorkbook var8 = new HSSFWorkbook();
            HSSFSheet var9 = null;
            HSSFRow var10 = null;
            HSSFCell var11 = null;
            HSSFCellStyle var12 = null;
            String var13 = var1.getHeader("user-agent");
            Hashtable var14 = this.initStyle(var4, var8);
            int var15 = 0;
            Set var16 = var4.getInfoindex();
            Properties var17 = System.getProperties();
            String var18 = var17.getProperty("file.encoding");

            while(true) {
                String var19;
                do {
                    if (!var4.next()) {
                        ByteArrayOutputStream var34 = new ByteArrayOutputStream();
                        var8.write(var34);
                        ByteArrayInputStream var35 = new ByteArrayInputStream(var34.toByteArray());
                        return var35;
                    }

                    var19 = var4.getSheetname();
                    var6 = var4.getSheet();
                } while(var6 == null);

                var9 = var8.createSheet();
                HSSFCellStyle var20 = var8.createCellStyle();
                HSSFDataFormat var21 = var8.createDataFormat();
                var20.setDataFormat(var21.getFormat("@"));
                var8.setSheetName(var15, Util.fromScreen2(this.handleSlash(var19), 7));
                ++var15;
                HSSFCellStyle var22 = var8.createCellStyle();
                var22.setDataFormat(var8.createDataFormat().getFormat("@"));

                int var23;
                for(var23 = 0; var23 < var6.size(); ++var23) {
                    var7 = var6.getExcelRow(var23);
                    if (var7 != null) {
                        short var24 = var7.getHight();
                        var10 = var9.createRow((short)var23);
                        if (var24 != 255) {
                            var10.setHeightInPoints((float)var24);
                        }

                        int var25 = 0;
                        boolean var26 = false;
                        boolean var27 = false;
                        if (var7.stylesize() == var7.size()) {
                            var26 = true;
                        }

                        if (var7.spansize() == var7.size()) {
                            var27 = true;
                        }

                        for(int var28 = 0; var28 < var7.size(); ++var28) {
                            var9.setDefaultColumnStyle(var28, var20);
                            var11 = var10.createCell((short)var25);
                            String var29 = Util.null2String(var7.getValue(var28));
                            var29 = Util.formatMultiLang(var29, var5);
                            String var30 = var29.substring(0, 2);
                            String var31 = var29.substring(2);
                            int var32;
                            if (var30.indexOf("s_") == 0) {
                                var11.setCellStyle(var22);
                                var11.setCellType(CellType.STRING);
                                var11.setCellValue(new String(Util.fromScreen4(var31, 7).getBytes(), var18));
                            } else if (var30.indexOf("i_") == 0) {
                                var32 = Util.getIntValue(var31, 0);
                                if (var32 != 0) {
                                    var11.setCellValue((double)var32);
                                }
                            } else if (var30.indexOf("f_") == 0) {
                                float var36 = Util.getFloatValue(var31);
                                if ((double)var36 != 0.0D) {
                                    var11.setCellValue((double)var36);
                                }
                            } else if (var30.indexOf("d_") == 0) {
                                double var37 = Util.getDoubleValue(var31, 0.0D);
                                if (var37 != 0.0D) {
                                    var11.setCellValue(var37);
                                }
                            } else if (var30.indexOf("o_") == 0) {
                                var11.setCellFormula(var31);
                            } else if (var30.indexOf("n_") == 0) {
                                var11.setCellValue(Util.fromScreen4(var31, 7));
                            }

                            if (var26) {
                                String var38 = Util.null2String(var7.getStyle(var28));
                                if (!var38.equals("")) {
                                    var12 = (HSSFCellStyle)var14.get(var38);
                                    if (var12 != null) {
                                        var11.setCellStyle(var12);
                                    }
                                }
                            }

                            if (var27) {
                                var32 = var7.getSpan(var28);
                                if (var32 > 1) {
                                    for(int var33 = 0; var33 < var32 - 1; ++var33) {
                                        ++var25;
                                        var11 = var10.createCell((short)var25);
                                        var11.setCellValue("");
                                        if (var26 && var12 != null) {
                                            var11.setCellStyle(var12);
                                        }
                                    }

                                    CellRangeAddress var39 = new CellRangeAddress(var23, var25 + 1 - var32, var23, var25);
                                    var9.addMergedRegion(var39);
                                }
                            }

                            ++var25;
                        }
                    }
                }

                for(var23 = 0; var23 < var6.columnsize(); ++var23) {
                    var9.setColumnWidth((short)var23, var6.getColumnwidth(var23));
                }
            }
        }
    }

    private ExcelFile createExcelFile(HttpServletRequest var1, HttpServletResponse var2, User var3) throws Exception {
        ResourceComInfo var4 = new ResourceComInfo();
        DepartmentComInfo var5 = new DepartmentComInfo();
        SubCompanyComInfo var6 = new SubCompanyComInfo();
        HashMap var7 = FnaCostCenter.getAllFnaCostCenterRecord();
        RecordSet var8 = new RecordSet();
        int var9 = Util.getIntValue(var1.getParameter("requestid"));
        int var10 = Util.getIntValue(var1.getParameter("workflowid"));
        int var11 = Util.getIntValue(var1.getParameter("nodeid"), -1);
        var11 = FreeNodeBiz.getExtendNodeId(var9, var11);
        String var12 = "";
        int var13 = 0;
        String var14 = "select formid,isbill,workflowname,isImportDetail from workflow_base where isImportDetail<>3 and id= ?";
        var8.executeQuery(var14, new Object[]{var10});
        boolean var15 = false;
        if (var8.next()) {
            int var16 = Util.getIntValue(var8.getString("isImportDetail"), 0);
            RequestDetailImport var17 = new RequestDetailImport();
            var17.getImportRight(var9, var11, var3.getUID(), var16);
        }

        String var54 = "/images/hdMaintenance_wev8.gif";
        String var55 = SystemEnv.getHtmlLabelName(26255, var3.getLanguage());
        String var18 = "";
        String var19 = "";
        ExcelSheet var20 = null;
        String var21 = "";
        boolean var22 = true;
        boolean var23 = false;
        int var56 = var8.getInt("formid");
        int var57 = var8.getInt("isbill");
        var21 = var8.getString("workflowname") + SystemEnv.getHtmlLabelName(64, var3.getLanguage());
        var21 = Util.formatMultiLang(var21, String.valueOf(var3.getLanguage()));
        ArrayList var24 = new ArrayList();
        int var25 = 0;
        var8.executeQuery("select ismode,showdes,printdes,toexcel from workflow_flownode where workflowid= ? and nodeid= ?", new Object[]{var10, var11});
        if (var8.next()) {
            var12 = Util.null2String(var8.getString("ismode"));
            var25 = Util.getIntValue(Util.null2String(var8.getString("showdes")), 0);
        }

        if (var12.equals("1") && var25 != 1) {
            var8.executeQuery("select id from workflow_nodemode where isprint='0' and workflowid=? and nodeid= ? ", new Object[]{var10, var11});
            if (var8.next()) {
                var13 = var8.getInt("id");
            } else {
                var8.executeQuery("select id from workflow_formmode where isprint='0' and formid= ? and isbill= ? ", new Object[]{var56, var57});
                if (var8.next()) {
                    var13 = var8.getInt("id");
                }
            }
        }

        if (var12.equals("1") && var13 > 0) {
            var14 = "select fieldid from workflow_modeview where isedit='1' and formid= ? and isbill= ? and nodeid= ? ";
            var8.executeQuery(var14, new Object[]{var56, var57, var11});
        } else {
            var14 = "select fieldid from workflow_nodeform where isedit='1' and nodeid= ? ";
            var8.executeQuery(var14, new Object[]{var11});
        }

        while(var8.next()) {
            var24.add("field" + var8.getString("fieldid"));
        }

        FnaCommon var26 = new FnaCommon();
        Hashtable var27 = new Hashtable();
        var27.put("hrmid", var3.getUID() + "");
        var27.put("reqid", var9 + "");
        var26.loadWFLayoutToHtmlFnaInfo(var56, var10, var9, var27);
        HashMap var28 = (HashMap)var27.get("_isEnableFnaWfHm_FnaCommon.getIsEnableFnaWfHm_workflowid=" + var10 + "__requestId=" + var9);
        if (var28 == null) {
            new HashMap();
        }

        HashMap var29 = (HashMap)var27.get("_isEnableFnaWfHm_fnaBudgetControl.getFnaWfFieldInfo4Expense_workflowid=" + var10 + "__requestId=" + var9);
        if (var29 == null) {
            var29 = new HashMap();
        }

        HashMap var30 = (HashMap)var27.get("_isEnableFnaWfHm_FnaCommon.qryFnaExpenseRequestRecord_workflowid=" + var10 + "__requestId=" + var9);
        if (var30 == null) {
            var30 = new HashMap();
        }

        String var31 = (String)var29.get("fnaWfType");
        ExcelFile var32 = new ExcelFile();
        var32.init();
        var32.setFilename(var21);
        ExcelStyle var33 = var32.newExcelStyle("Header");
        var33.setGroundcolor(ExcelStyle.WeaverHeaderGroundcolor);
        var33.setFontcolor(ExcelStyle.WeaverHeaderFontcolor);
        var33.setFontbold(ExcelStyle.WeaverHeaderFontbold);
        var33.setAlign(ExcelStyle.WeaverHeaderAlign);
        FieldInfo var34 = new FieldInfo();
        var34.setRequestid(var9);
        var34.setUser(var3);
        var34.GetDetailTableField(var56, var57, var3.getLanguage());
        FieldInfo var35 = new FieldInfo();
        var35.setRequestid(var9);
        var35.setUser(var3);
        var35.GetDetailTblFields(var56, var57, var3.getLanguage());
        ArrayList var36 = var34.getDetailTableFieldNames();
        ArrayList var37 = var34.getDetailTableFields();

        for(int var38 = 0; var38 < var36.size(); ++var38) {
            var20 = new ExcelSheet();
            ExcelRow var39 = var20.newExcelRow();
            ArrayList var40 = (ArrayList)var36.get(var38);
            ArrayList var41 = (ArrayList)var37.get(var38);
            boolean var42 = false;
            var20.addColumnwidth(3000);
            var39.addStringValue(SystemEnv.getHtmlLabelName(15486, var3.getLanguage()), "Header");

            int var43;
            for(var43 = 0; var43 < var41.size(); ++var43) {
                if (var24.indexOf((String)Util.TokenizerString((String)var41.get(var43), "_").get(0)) >= 0) {
                    String[] var44 = Util.TokenizerString2((String)var41.get(var43), "_");
                    if (!"6".equals(var44[3])) {
                        var20.addColumnwidth(6000);
                        var39.addStringValue((String)var40.get(var43), "Header");
                        var42 = true;
                    }
                }
            }

            if (var42) {
                var20.addExcelRow(var39);

                for(var43 = 0; var43 < var35.getRowSize(var38); ++var43) {
                    var39 = var20.newExcelRow();
                    var20.addColumnwidth(3000);
                    var39.addStringValue(var43 + 1 + "", "Border");

                    for(int var58 = 0; var58 < var41.size(); ++var58) {
                        if (var24.indexOf((String)Util.TokenizerString((String)var41.get(var58), "_").get(0)) >= 0) {
                            String[] var45 = Util.TokenizerString2((String)var41.get(var58), "_");
                            if (!"6".equals(var45[3])) {
                                var20.addColumnwidth(6000);
                                String var46 = var35.getFieldExportCellValue(var34, var38, var43, var58);
                                String var47 = "";
                                int var48 = 0;
                                String var49 = "";

                                try {
                                    var47 = (String)var41.get(var58);
                                } catch (Exception var53) {
                                }

                                try {
                                    var48 = Util.getIntValue(var47.split("_")[0].replaceAll("field", ""));
                                } catch (Exception var52) {
                                }

                                try {
                                    String var10000 = (String)((List)var34.getDetailDBFieldNames().get(var38)).get(var58);
                                } catch (Exception var51) {
                                }

                                var46 = FnaCommon.getOrgNameByOrgType(var46, var48, var9, var43, var31, var29, var30, var27, var3.getLanguage(), var4, var5, var6, var7);
                                var39.addStringValue(var46, "Border");
                            }
                        }
                    }

                    var20.addExcelRow(var39);
                }

                Map<String ,Object> params= ParamUtil.request2Map(var1);
                int dt=Util.getIntValue(Util.null2String(params.get("dtno")),-1);
                String name=Util.null2String(params.get("sheetname"));
                Console.log("params:"+params);
                if(dt>-1){
                    if(name.equals("")){
                        var32.setFilename(var21);
                    }
                    if(var38+1==dt) {
                        var32.addSheet(SystemEnv.getHtmlLabelName(17463, var3.getLanguage()) + (var38 + 1), var20);
                    }
                }else{
                    var32.addSheet(SystemEnv.getHtmlLabelName(17463, var3.getLanguage()) + (var38 + 1), var20);
                }
            }
        }

        return var32;
    }

    private Hashtable initStyle(ExcelFile var1, HSSFWorkbook var2) {
        Hashtable var3 = new Hashtable();
        HSSFCellStyle var4 = null;
        HSSFFont var5 = null;

        while(var1.nextStyle()) {
            String var6 = var1.getStyleName();
            ExcelStyle var7 = var1.getStyleValue();
            if (var7 != null) {
                var4 = var2.createCellStyle();
                var5 = var2.createFont();
                if (var7.getGroundcolor() != 9) {
                    var4.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                    var4.setFillForegroundColor(var7.getGroundcolor());
                }

                var4.setRotation(var7.getScale());
                if (var7.getAlign() != 10) {
                    var4.setAlignment(HorizontalAlignment.forInt(var7.getAlign()));
                }

                if (var7.getDataformart() != 0) {
                    var4.setDataFormat(var7.getDataformart());
                }

                var4.setVerticalAlignment(VerticalAlignment.forInt(var7.getValign()));
                var5.setColor(var7.getFontcolor());
                var5.setBold(var7.getFontbold() == 700);
                var5.setFontHeightInPoints(var7.getFontheight());
                var4.setFont(var5);
                BorderStyle var8 = BorderStyle.valueOf(var7.getCellBorder());
                var4.setBorderTop(var8);
                var4.setBorderLeft(var8);
                var4.setBorderRight(var8);
                var4.setBorderBottom(var8);
                var3.put(var6, var4);
            }
        }

        return var3;
    }

    private String handleSlash(String var1) {
        return var1.replaceAll("/", "");
    }

    public String getFilename() {
        return this.filename;
    }
}
